package com.atguigu.statservice.service.impl;

import com.atguigu.commonutils.R;
import com.atguigu.statservice.client.UcenterClient;
import com.atguigu.statservice.entity.StatisticsDaily;
import com.atguigu.statservice.mapper.StatisticsDailyMapper;
import com.atguigu.statservice.service.StatisticsDailyService;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.apache.commons.lang3.RandomUtils;
import org.bouncycastle.pqc.math.linearalgebra.RandUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 网站统计日数据 服务实现类
 * </p>
 *
 * @author 李川
 * @since 2020-05-22
 */
@Service
public class StatisticsDailyServiceImpl extends ServiceImpl<StatisticsDailyMapper, StatisticsDaily> implements StatisticsDailyService {

    @Autowired
    private UcenterClient ucenterClient;
    //统计某天注册人数
    @Override
    public void register(String day) {
        QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
        wrapper.eq("date_calculated",day);
        baseMapper.delete(wrapper);
        R r = ucenterClient.countRegister(day);
        Integer countRegister = (Integer) r.getData().get("countRegister");
        StatisticsDaily sta=new StatisticsDaily();
        sta.setRegisterNum(countRegister);
        sta.setDateCalculated(day);
        sta.setVideoViewNum(RandomUtils.nextInt(100,200));
        sta.setLoginNum(RandomUtils.nextInt(100,200));
        sta.setCourseNum(RandomUtils.nextInt(100,200));
        baseMapper.insert(sta);
    }

    //图表显示，日期数组，数量数组
    @Override
    public Map<String, Object> getShowData(String type, String begin, String end) {
       QueryWrapper<StatisticsDaily> wrapper=new QueryWrapper<>();
       wrapper.between("date_calculated",begin,end);
       wrapper.select("date_calculated",type);
        List<StatisticsDaily> list = baseMapper.selectList(wrapper);
        List<String> date=new ArrayList<>();
        List<Integer> numData=new ArrayList<>();
        for (int i = 0; i < list.size(); i++) {
            StatisticsDaily daily=list.get(i);
            date.add(daily.getDateCalculated());
            switch (type){
                case "login_num":numData.add(daily.getLoginNum());
                    break;
                case "register_num":numData.add(daily.getRegisterNum());
                    break;
                case "course_num":numData.add(daily.getCourseNum());
                    break;
                case "video_view_num":numData.add(daily.getVideoViewNum());
                    break;
                 default:
                    break;
            }
        }
        Map<String, Object> map=new HashMap<>();
        map.put("date_calculatedList",date);
        map.put("numDataList",numData);
        return map;
    }
}
